$('[name="login"]').click(() => {
    login();
    return false;
})
$('[name="register"]').click(() => {
    $('[name="register"]').preventDefault();
    return false;
})

async function login() {
    let res = await myPromiseAjax({
        url: '../server/goods_login.php',
        type: 'post',
        data: {
            userName: $('[type="text"]').val(),
            userPwd: $('[type="password"]').val(),
        },
    });
    res = JSON.parse(res);
    console.log(res);
    if (res.code === 0) {
        $('h2').html(`<span style="color:red;height: 35px;line-height: 35px;  width: 300px;
        border-radius: 15px;
        display: block;
        font-size: 20px;
        background-color: rgba(128, 101, 101, 0.201);text-align: center;">登录失败 请检查账号密码</span>`);

    } else if (res.code === 1) {

        mySetCookie({
            key: 'login',
            val: 1,
            path: '/',
            time: 7 * 24 * 60 * 60,
        })

        let num = 5;

        $('h2').html(`<span style="color:#adc9ff;  
        text-align: center;
        height: 35px;
        line-height: 35px;
        width: 300px;
        border-radius: 15px;
        display: block;
        font-size: 20px;
        background-color: rgba(128, 101, 101, 0.201);">登录成功 ${num}秒 后 跳转 <a href="./index.html">首页面</a></span>`);

        let time = setInterval(() => {
            num--;
            $('h2').html(`<span style="color:#adc9ff;  
            text-align: center;
            height: 35px;
            line-height: 35px;
            width: 300px;
            display: block;
            border-radius: 15px;
            font-size: 20px;
            background-color: rgba(128, 101, 101, 0.201);">登录成功 ${num}秒 后 跳转 <a href="./index.html">首页面</a></span>`);
            if (num === 1) {
                clearInterval(time);
                let url = window.location.search.substring(5);
                console.log(url);

                if (url === '123') {
                    // 从 注册页 过来的 设定指定的返回页面
                    window.location.href = './index.html';

                } else {
                    // 按照获取的地址跳转页面
                    // 哪儿来的回哪儿去
                    window.location.href = url;
                }
            }
        }, 1000)
    }
}




